package com.biostime.qdingding.modules.qd.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.service.CrudService;
import com.biostime.qdingding.modules.qd.api.dto.BalanceDto;
import com.biostime.qdingding.modules.qd.api.dto.TransrecordDto;
import com.biostime.qdingding.modules.qd.dao.TransrecordDao;
import com.biostime.qdingding.modules.qd.entity.Student;
import com.biostime.qdingding.modules.qd.entity.Transrecord;
import com.biostime.qdingding.modules.sys.entity.User;

/**
 * 收支记录Service
 * @author king
 * @version 2015-11-12
 */
@Service
@Transactional(readOnly = true)
public class TransrecordService extends CrudService<TransrecordDao, Transrecord> {
	@Autowired 
	private TransrecordDao recordDao;
	
	
	@Override
	public List<Transrecord> findList(Transrecord entity) {
		dataScopeFilter(entity, "dsf", "id=a.center_id", "id=a.create_by");
		return super.findList(entity);
	}
	
	@Override
	public Page<Transrecord> findPage(Page<Transrecord> page, Transrecord entity) {
		entity.setPage(page);
		page.setList(this.findList(entity));
		return page;
	}
	
	public List<Transrecord> getContractRecord(Transrecord transrecord){
		return recordDao.getContractRecord(transrecord);
	}
	
	
	
//	<!-- ————————————————————————————楚河汉界，谁敢动我的，打死 ————————————————————————————————————-->		
	public List<TransrecordDto> apiGetTransrecords(String studentId){
		return recordDao.apiGetTransrecords(studentId);
	}
	
	public BalanceDto getBalance(Student student){
		return dao.getBalance(student);
	}
	
	public TransrecordDto apiTransrecordByOrder(String studentId,String courseOrderId){
		return dao.apiTransrecordByOrder(studentId, courseOrderId);
	}
	
	/**
	 * 添加请假返款记录
	 * 返回涉及金额
	 * @param request
	 * @return
	 */
	public Double apiInsertTrans(HttpServletRequest request,String type){
		String userId = (String) request.getAttribute("userId");
		String studentId = (String) request.getAttribute("studentId");
		String contractId = (String) request.getAttribute("contractId");
		String orderId = (String) request.getAttribute("orderId");
		String centerId = (String) request.getAttribute("centerId");
		String str = (String) request.getAttribute("str");
		Transrecord tr = new Transrecord();
		tr.setInout(0);// 收支类型，0收，1支 (以会员的角度)		
		
		Double money = 0d;
		
		TransrecordDto transrecordDto = apiTransrecordByOrder(studentId,orderId);
		if(transrecordDto  != null){
			money = transrecordDto.getInvolveMoney();
		}
		tr.setMoney(money); // 交易金额
		
		/*1	微信支付
		2	支付宝支付
		3	银联在线支付
		4	系统返还
		5	系统扣费
		6	现金支付*/
		tr.setPayType("4"); //支付方式（微信，支付宝，银联，现金）
		tr.setPayDate(new Date()); //发生日期
		
		if(type.equals("leave")){
			//交易类型 支的有：普通课程、户外课程； 收的有：1充值（即合同支付）、2请假资格返还、3取消资格返还，
			tr.setType("1");
			tr.setDescription(String.format(str+"请假返款", tr.getMoney())); //描述
		}else if(type.equals("abort")){
			//交易类型 支的有：普通课程、户外课程； 收的有：1充值（即合同支付）、2请假资格返还、3取消资格返还，
			tr.setType("2");
			tr.setDescription(String.format(str+"中止返款", tr.getMoney())); //描述
		}
		
		tr.setMemberId(Integer.parseInt(userId)); //会员编号
		tr.setStudentId(Integer.parseInt(studentId)); //学员编号
		tr.setCourseorderId(Integer.parseInt(orderId)); //预约课程编号
		tr.setContractId(Integer.parseInt(contractId)); //合同编号
		tr.setInvalid(0); //是否作废
		tr.setCenterId(Integer.parseInt(centerId));//早教中心
		tr.setCreateBy(new User("0"));
		tr.setCreateDate(new Date());
		tr.setUpdateBy(new User("0"));
		tr.setUpdateDate(new Date());
		save(tr);
		
		return money;
	}
	
	public void apiPay(Transrecord tr){
		tr.setInout(0);// 收支类型，0收，1支 (以会员的角度)				
		tr.setPayDate(new Date()); //发生日期		
		tr.setCourseorderId(0); //预约课程编号
		tr.setInvalid(0); //是否作废
		tr.setCreateBy(new User("0"));
		tr.setCreateDate(new Date());
		tr.setUpdateBy(new User("0"));
		tr.setUpdateDate(new Date());
		save(tr);		
	}
	
	public Transrecord apiGetByCode(String code){
		return dao.apiGetByCode(code);
	}
	public Double apiGetRecharge(String studentId,String contractId){
		return dao.apiGetRecharge(studentId, contractId);
	}
}
